package com.epfresh.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.bean.ListSecondKill;
import com.epfresh.utils.FormatUtil;
import com.epfresh.utils.TagsUtils;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class SecondKillListAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;


    private List<ListSecondKill.ContentBean> list;
    private AdapterView.OnItemClickListener onAddClickListener;
    private String acceptTime;
    private SecondKillListAdapter.OnTextChangeListener onTextChangeListener;
    private SecondKillListAdapter.OnTextChangeListener onChangeListener;
    boolean isShowMarketName;

    OnCartAddListener onCartAddListener;

    public OnTextChangeListener getOnTextChangeListener() {
        return onTextChangeListener;
    }

    public void setOnTextChangeListener(OnTextChangeListener onTextChangeListener) {
        this.onTextChangeListener = onTextChangeListener;
    }
    public OnTextChangeListener getOnChangeListener() {
        return onChangeListener;
    }

    public void setOnChangeListener(OnTextChangeListener onChangeListener) {
        this.onChangeListener = onChangeListener;
    }


    public SecondKillListAdapter(Context context, List<ListSecondKill.ContentBean> list) {
        this.context = context;
        this.list = list;


    }

    public interface OnCheckChangeListener {
        public void onCheckChange(boolean isCheck, int groupPosition, int childPosition);
    }

    public interface OnTextChangeListener {
        public void onTextChange(int groupPosition, int childPosition, double lastCnt,  ListSecondKill.ContentBean cartGood);
    }



    public OnCartAddListener getOnCartAddListener() {
        return onCartAddListener;
    }

    public void setOnCartAddListener(OnCartAddListener onCartAddListener) {
        this.onCartAddListener = onCartAddListener;
    }

    public boolean isShowMarketName() {
        return isShowMarketName;
    }

    public void setShowMarketName(boolean showMarketName) {
        isShowMarketName = showMarketName;
    }

    public String getAcceptTime() {
        return acceptTime;
    }

    public void setAcceptTime(String acceptTime) {
        this.acceptTime = acceptTime;
    }

    public boolean isShowTime() {
        if (acceptTime == null || "".equals(acceptTime)) {
            return false;
        }
        return true;
    }


    public AdapterView.OnItemClickListener getOnAddClickListener() {
        return onAddClickListener;
    }

    public void setOnAddClickListener(AdapterView.OnItemClickListener onAddClickListener) {
        this.onAddClickListener = onAddClickListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh = null;
        if (view == null) {
            vh = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_list_second, null, false);
//            vh.tvType = (TextView) view.findViewById(R.id.tv_type);
            vh.ll_tags = (LinearLayout) view.findViewById(R.id.ll_tags);
            vh.tvTitle = (TextView) view.findViewById(R.id.tv_product_title);
            vh.tvPreTitle = (TextView) view.findViewById(R.id.tv_pre_product_title);
            vh.tv_status_flag = (TextView) view.findViewById(R.id.tv_status_flag);
            vh.tvTime = (TextView) view.findViewById(R.id.tv_accept_time);
            vh.tvUnit = (TextView) view.findViewById(R.id.tv_unit);
            vh.tvCount = (TextView) view.findViewById(R.id.ed_price);
            vh.tvUnitPackage = (TextView) view.findViewById(R.id.tv_unit_package);
            vh.iv = (ImageView) view.findViewById(R.id.iv_product);
            vh.iv_tag = (ImageView) view.findViewById(R.id.iv_tag);
            vh.ivAdd = (ImageView) view.findViewById(R.id.iv_add);
            vh.iv_add_grey = (ImageView) view.findViewById(R.id.iv_add_grey);
            vh.tv_promotion_limit = (TextView)view.findViewById(R.id.tv_promotion_limit);
            vh.edAmount = (TextView) view.findViewById(R.id.ed_price);

            //vh.tvCountAdd = (TextView) view.findViewById(R.id.tv_count);
            vh.tvChargeUnit = (TextView) view.findViewById(R.id.tv_charge_unit);
            vh.tvDiscountTitle = (TextView) view.findViewById(R.id.tv_discount_title);
            vh.tvDiscountTitle.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            vh.ivCountAdd = (ImageView) view.findViewById(R.id.iv_add_count);
            vh.ivCountRemove = (ImageView) view.findViewById(R.id.iv_remove_count);
//            view.findViewById(R.id.v_cart).setOnClickListener(this);

            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        vh.tvTitle.setTag(R.id.item_key_position,i);
        vh.tvTitle.setOnClickListener(this);
        ListSecondKill.ContentBean p = list.get(i);
        ImageLoader.getInstance().displayImage(p.getProduct().getThumbnail(), vh.iv);
        if (p.getPromotionMarkUrl() != null && !p.getPromotionMarkUrl().equals("") && !p.getPromotionMarkUrl().equals("null"))
            ImageLoader.getInstance().displayImage(p.getPromotionMarkUrl(), vh.iv_tag);
        else
            vh.iv_tag.setVisibility(View.INVISIBLE);


        vh.tvTitle.setText(p.getProduct().getProductParseDto().getTitle());
        vh.tvUnit.setText(p.getPrice()+"元/"+p.getProduct().getChargeUnit());
        vh.tvDiscountTitle.setText("原价:"+p.getProduct().getProductSpecPrice().getPrice() + "元/"+p.getProduct().getChargeUnit());
        vh.tvChargeUnit.setText(p.getProduct().getChargeUnit());
        String pack = p.getProduct().getPack();
        if (pack == null || "".equals(pack)) {

            vh.tvUnitPackage.setVisibility(View.INVISIBLE);
        } else {
            vh.tvUnitPackage.setVisibility(View.VISIBLE);
        }

        vh.edAmount.setTag(R.id.item_key_position, i);

        vh.edAmount.setText(FormatUtil.subZeroAndDot(p.getCount() + ""));
        vh.edAmount.setOnClickListener(this);


        vh.tvUnitPackage.setText(pack);
        vh.ivAdd.setTag(R.id.item_key_position, i);
        vh.ivAdd.setOnClickListener(this);
        vh.tvCount.setTag(R.id.item_key_position, i);
        vh.ivCountAdd.setTag(R.id.item_key_position, i);
        vh.ivCountRemove.setTag(R.id.item_key_position, i);
        vh.iv.setTag(R.id.item_key_position, i);
        vh.tvCount.setOnClickListener(this);
        vh.ivCountAdd.setOnClickListener(this);
        vh.ivCountRemove.setOnClickListener(this);
        vh.iv.setOnClickListener(this);
        if (p.getProduct().getTags() != null && p.getProduct().getTags().size() > 0) {
            vh.ll_tags.setVisibility(View.VISIBLE);
            TagsUtils.initTags(vh.ll_tags, p.getProduct().getTags());
        } else {
            vh.ll_tags.setVisibility(View.GONE);
        }
        vh.ivAdd.setVisibility(View.INVISIBLE);
        vh.ivCountAdd.setVisibility(View.INVISIBLE);
        vh.tv_status_flag.setVisibility(View.INVISIBLE);
        vh.ivCountRemove.setVisibility(View.INVISIBLE);
        vh.tvChargeUnit.setVisibility(View.INVISIBLE);
        vh.iv_add_grey.setVisibility(View.INVISIBLE);
        vh.tvCount.setVisibility(View.INVISIBLE);
        vh.tv_promotion_limit.setVisibility(View.INVISIBLE);
        if(p.getQuantityPerAccount()!=0){
            vh.tv_promotion_limit.setVisibility(View.VISIBLE);
            vh.tv_promotion_limit.setText("限购"+p.getQuantityPerAccount()+p.getProduct().getChargeUnit());
            GradientDrawable myGrad = (GradientDrawable)vh.tv_promotion_limit.getBackground();
            myGrad.setColor(Color.parseColor("#70CCAD"));
        }else{
            vh.tv_promotion_limit.setVisibility(View.GONE);
        }

        if(p.getIsAcceptOrders()==0&&p.getStatus()==2){
            p.setStatus(1);
        }
        if(p.getStatus()==1){
            vh.iv_add_grey.setVisibility(View.VISIBLE);
        }else if(p.getStatus()==3){
            vh.tv_status_flag.setText("结束了");
            vh.tv_status_flag.setVisibility(View.VISIBLE);
        }else if(p.getStatus()==4){
            vh.tv_status_flag.setText("抢光了");
            vh.tv_status_flag.setVisibility(View.VISIBLE);
        }else{
            if(p.getCount()==0){
                vh.ivAdd.setVisibility(View.VISIBLE);
                if(p.getQuantityPerAccount()!=0) {
                    vh.tv_promotion_limit.setVisibility(View.VISIBLE);
                    vh.tv_promotion_limit.setText("限购" + p.getQuantityPerAccount() + p.getProduct().getChargeUnit());
                }else{
                    vh.tv_promotion_limit.setVisibility(View.GONE);
                }
            }
            else {
                vh.ivCountAdd.setVisibility(View.VISIBLE);
                vh.ivCountRemove.setVisibility(View.VISIBLE);
                vh.tvCount.setText(FormatUtil.subZeroAndDot(p.getCount()+""));
                vh.tvCount.setVisibility(View.VISIBLE);
                vh.tv_promotion_limit.setVisibility(View.VISIBLE);
                vh.tv_promotion_limit.setText("秒杀");
                GradientDrawable myGrad = (GradientDrawable)vh.tv_promotion_limit.getBackground();
                myGrad.setColor(Color.parseColor("#1fadee"));
                vh.tvChargeUnit.setVisibility(View.VISIBLE);
            }
        }
        return view;
    }

    @Override
    public void onClick(View v) {


        Object o = v.getTag(R.id.item_key_position);

        if (o != null && o instanceof Integer) {
            int position = (int) o;
            if ((v.getId() == R.id.iv_add)) {

                if (onAddClickListener != null) {
                    onAddClickListener.onItemClick(null, v, position, position);
                }

            } else if (v.getId() == R.id.ed_price) {//购物车手动输入

                if (onCartAddListener != null&& position < list.size()) {
                    onCartAddListener.onCartAdd(1, position, list.get(position),v);
                }

            } else if (v.getId() == R.id.iv_add_count) {//添加
                if (onCartAddListener != null&& position < list.size()) {
                    onCartAddListener.onCartAdd(2, position, list.get(position),v);
                }

            } else if (v.getId() == R.id.iv_remove_count) {//减少
                if (onCartAddListener != null && position < list.size()) {
                    onCartAddListener.onCartAdd(3, position, list.get(position),v);
                }

            } else if (v.getId() == R.id.tv_product_title||v.getId() == R.id.iv_product) {//
                if (onCartAddListener != null&& position < list.size()) {
                    onCartAddListener.onCartAdd(5, position, list.get(position),v);
                }

            }

        }
    }

    public interface OnCartAddListener {
        public void onCartAdd(int type, int position, ListSecondKill.ContentBean goods, View v);
    }


    public class ViewHolder {
        //        TextView tvType;
        TextView tv_promotion_limit;
        LinearLayout ll_tags;
        TextView tvTitle;
        TextView tvPreTitle;
        TextView tvUnit;
        TextView tvUnitPackage;
        //        TextView tvCount;
        TextView tvTime;
        //TextView tvMarketName;
        ImageView iv;
        ImageView iv_tag;
        ImageView ivAdd;
        ImageView iv_add_grey;
        TextView edAmount;


        ImageView ivCountAdd;
        ImageView ivCountRemove;
        TextView tvCount;
        TextView tvChargeUnit;
        TextView tv_status_flag;
        TextView tvDiscountTitle;
        TextView tvDiscountRate;

        public void showCartAddView(boolean isShow) {
            if (isShow) {
                ivCountAdd.setVisibility(View.VISIBLE);
                ivCountRemove.setVisibility(View.VISIBLE);
                tvCount.setVisibility(View.VISIBLE);
                tvChargeUnit.setVisibility(View.VISIBLE);
                ivAdd.setVisibility(View.GONE);

            } else {
                ivCountAdd.setVisibility(View.GONE);
                ivCountRemove.setVisibility(View.GONE);
                tvCount.setVisibility(View.GONE);
                tvChargeUnit.setVisibility(View.GONE);

                ivAdd.setVisibility(View.VISIBLE);
            }
        }


    }


}
